package com.rpay.admin.util.render;

/**
 * Created by Administrator on 2017/5/17 0017.
 */


import com.rpay.admin.util.http.exception.WebException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

public abstract class Render {

    /**
     * Render to client
     */
    public abstract void render(HttpServletRequest request, HttpServletResponse response, Object out);

    public void write(HttpServletRequest request, HttpServletResponse response, String content) {
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.print(content);
        } catch (IOException e) {
            throw new WebException(e.getMessage());
        }
    }

    public void write(HttpServletRequest request, HttpServletResponse response, String type, RenderedImage content) {
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ImageIO.write(content, type, outputStream);
        } catch (Exception e) {
            throw new WebException(e.getMessage());
        }
    }
}
